package cn.abcsys.cloud.devops.monitor.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.monitor.core.MonitorCore;
import cn.abcsys.cloud.devops.monitor.service.MonitorService;
import cn.abcsys.cloud.devops.monitor.utils.Result;

/**
 * 监控控制器
 * 
 * @author mayunhao
 *
 */
@RestController
@RequestMapping("/monitor")
public class MonitorController {
	@Autowired
	private MonitorCore monitorCore;
	@Autowired
	private MonitorService monitorService;

	@RequestMapping(value = "/history", method = { RequestMethod.GET })
	@ResponseBody
	public Result history(@RequestParam Map<String, Object> params) {
		if (params.isEmpty()) {
			return new Result(false, "请求参数为空");
		} else {
			JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
			// paramIn = new JSONObject();
			// //监控主机名称
			// paramIn.put("objectName", "boc-dev-server001");
			// //监控项
			// paramIn.put("MonitorItem", "system.cpu.load");
			// //起止时间
			// paramIn.put("time_from", 1497856440);
			// paramIn.put("time_till", 1498029240);
			return monitorCore.hostMonitor(paramIn);
		}
	}

	@RequestMapping(value = "/immediate", method = { RequestMethod.GET })
	@ResponseBody
	public Result immediate(@RequestParam Map<String, Object> params) {
		if (params.isEmpty()) {
			return new Result(false, "请求参数为空");
		} else {
			JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
			return monitorCore.zabbixGetImmediateData(paramIn);
		}
	}

	@RequestMapping(value = "/test")
	public void test() {
		monitorService.run();
	}
}
